package com.glowdayz.glowmee;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewProductFm extends Fragment {
	
	String PARAM_volume = "", PARAM_keyWord = "", PARAM_productText = "", PARAM_productTitle = "", PARAM_brandTitle = "";
    int PARAM_price = 0;
    
    TextView volume = null, price = null, keyWord = null, productText = null;
    ImageView lowPriceBt = null;
    Button productTextMore = null;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        View viewProductView = inflater.inflate(R.layout.fm_view_product, container, false);
        
        PARAM_price = getArguments().getInt("PARAM_price");
        PARAM_volume = getArguments().getString("PARAM_volume");
        PARAM_keyWord = getArguments().getString("PARAM_keyWord");
        PARAM_productText = getArguments().getString("PARAM_productText");
        PARAM_productTitle = getArguments().getString("PARAM_productTitle");
        PARAM_brandTitle = getArguments().getString("PARAM_brandTitle");
        
        lowPriceBt = (ImageView) viewProductView.findViewById(R.id.lowPriceBt);
        lowPriceBt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return false;
            }
        });
        lowPriceBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), WebPage.class);
				intent.putExtra("PARAM_bannerUrl", "http://m.shopping.naver.com/search/all_search.nhn?query=" + 
				PARAM_brandTitle + " " + PARAM_productTitle + "&sort=price_asc");
				startActivity(intent);
			}
		});
        
        productTextMore = (Button) viewProductView.findViewById(R.id.productTextMore);
        
        
        volume = (TextView) viewProductView.findViewById(R.id.volume);
        price = (TextView) viewProductView.findViewById(R.id.price);
        keyWord = (TextView) viewProductView.findViewById(R.id.keyWord);
        productText = (TextView) viewProductView.findViewById(R.id.productText);
        
        volume.setText(PARAM_volume);
        price.setText(PARAM_price + "원");
        keyWord.setText(PARAM_keyWord);
        productText.setText(PARAM_productText);
        
        productText.post(new Runnable() {
            @Override
            public void run() {
                final int lineCount = productText.getLineCount();
                if (lineCount > 4) {
                	productTextMore.setVisibility(View.VISIBLE);
                	productTextMore.setOnClickListener(new OnClickListener() {
        				@Override
        				public void onClick(View arg0) {
        					productText.setMaxLines(lineCount);
        					productTextMore.setVisibility(View.GONE);
        				}
        			});
                }
            }
        });
        
        return viewProductView;
    }
}
